<%@ page import="bean.Dep" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="bean.Student" %>
<%@ page import="bean.Course" %>
<%@ page import="bean.SC" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="utf-8">
    <title>选课信息添加页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="./css/x-admin.css" media="all">
</head>

<body>
<%
    ArrayList<Student> students = (ArrayList<Student>) request.getAttribute("student_list");
    ArrayList<Course> courses = (ArrayList<Course>) request.getAttribute("course_list");
%>
<div class="x-body">
    <form class="layui-form" method="post">
        <div class="layui-form-item">
            <label for="sno" class="layui-form-label">
                <span class="x-red">*</span>学号
            </label>
            <div class="layui-input-inline">
                <input type="text" id="sno" name="sno" required="" lay-verify="check_sno" autocomplete="off"
                       class="layui-input" value="" disabled>
            </div>
            <div class="layui-form-mid layui-word-aux" style="display: none">
                <span class="x-red">*</span>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="sname" class="layui-form-label">
                <span class="x-red">*</span>姓名
            </label>
            <div class="layui-input-inline" tabindex="-1" onblur="changeSno()">
<%--            <div class="layui-input-inline" tabindex="-1" onblur="changeSno()">--%>
                <label>
                    <select id="sname" name="sname" lay-verify="check_stu">
                        <option value="">请选择学生</option>
                        <c:forEach var="students" items="student_ist">
                            <%for (Student student : students) {%>
                            <option value="<%=student.getSno()%>"><%=student.getSname()%>(<%=student.getSno()%>)
                            </option>
                            <%}%>
                        </c:forEach>
                    </select>
                </label>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="course" class="layui-form-label">
                <span class="x-red">*</span>课程
            </label>
            <div id="course" class="layui-input-inline">
                <label>
                    <select id="cno" name="cno" lay-verify="check_course">
                        <option value="">请选择课程</option>
                        <c:forEach var="course" items="course_list">
                            <%for (Course course : courses) {%>
                            <option value="<%=course.getCno()%>"><%=course.getCname()%>
                            </option>
                            <%}%>
                        </c:forEach>
                    </select>
                </label>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="add" class="layui-form-label">
            </label>
            <button id="add" class="layui-btn" lay-filter="add" lay-submit="">
                添加
            </button>
        </div>

    </form>
</div>
<script src="./lib/layui/layui.js" charset="utf-8"></script>
<script src="./js/x-layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form(),
            layer = layui.layer;

        // 自定义验证规则
        form.verify({
            check_stu: function (value) {
                if (value.length < 1) {
                    return '姓名不能为空';
                }
            },
            check_course: function (value) {
                if (value.length < 1) {
                    return '课程不能为空';
                }
            }
        });

        //监听提交
        form.on('submit(add)', function (data) {
            layer.confirm('确定添加吗？', {icon: 3, title: '提示添加'}, function (index) {
                if ((data.field.sno).length > 1 && (data.field.cno).length > 1) {
                    $.ajax({
                        url: "/sc_add",
                        type: "post",
                        data: data.field,
                        success: function (data) {
                            let data_json = JSON.parse(data);
                            if (data_json.code === 200) {
                                // layer.msg(data.msg, {icon: 6});
                                layer.alert(data_json.msg, function () {
                                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                                    parent.location.reload();//刷新父页面
                                });
                            } else {
                                layer.msg(data_json.msg, {icon: 5});
                            }
                        }
                    });
                }
            });
            return false;
        });
    });

    function changeSno() {
        document.getElementById("sno").value = document.getElementById("sname").value;

    }
</script>
<script>
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>
</body>

</html>